<template>
	<view class="u-page">

		<view class="content">
			<view class="tasklist">
				<view class="lable">
					<view class="lab-title">订单任务(线下) </view>
					<view class="addtask" @click="addItem()">
						<image src="../../static/icon/addtask.png" mode=""></image>
					</view>
				</view>
				<view class="task" v-for="(item,key) in task2" :key="key" v-if="task2.length > 0">
					<view class="taskinfo" v-if="item.online ==1">消费<input type="digit" v-model="item.where1"
							class="input" @input="onKeyInput" />单&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;奖励<input
							type="digit" class="input" v-model="item.reward" @input="reward" />元</view>
				</view>

			</view>

			<view class="tasklist">
				<view class="lable">
					<view class="lab-title">订单任务(线上) </view>
					<view class="addtask" @click="online()">
						<image src="../../static/icon/addtask.png" mode=""></image>
					</view>
				</view>
				<view class="task" v-for="(item,key) in task2" :key="key" v-if="task2.length > 0">
					<view class="taskinfo" v-if="item.online ==2">消费<input type="digit" class="input"
							v-model="item.where1"
							@input="onKeyInput" />单&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;奖励<input type="digit"
							class="input" v-model="item.reward" @input="reward" />元</view>
					<view class="addtask" v-if="item.online ==2 && key==0" @click="online()">
						<image src="../../static/icon/addtask.png" mode=""></image>
					</view>
				</view>

			</view>
			<view class="tasklist">
				<view class="lable">
					<view class="lab-title">金额任务(线上) </view>
					<view class="addtask" @click="priceAddItem()">
						<image src="../../static/icon/addtask.png" mode=""></image>
					</view>
				</view>
				<view class="task" v-for="(item,key) in task1" :key="key" v-if="task1.length > 0">
					<view class="taskinfo" v-if="item.online ==1">消费<input type="digit" class="input"
							v-model="item.where1"
							@input="onKeyInput" />元&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;奖励<input type="digit"
							class="input" v-model="item.reward" @input="reward" />%消费金</view>

				</view>

			</view>
			<view class="tasklist">
				<view class="lable">
					<view class="lab-title">金额任务(线下) </view>
					<view class="addtask" @click="priceAddItemonline()">
						<image src="../../static/icon/addtask.png" mode=""></image>
					</view>
				</view>
				<view class="task" v-for="(item,key) in task1" :key="key" v-if="task1.length > 0">
					<view class="taskinfo" v-if="item.online ==2">消费<input type="digit" class="input"
							v-model="item.where1"
							@input="onKeyInput" />元&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;奖励<input type="digit"
							class="input" v-model="item.reward" @input="reward" />%消费金</view>
				</view>

			</view>
			<view class="tasklist">
				<view class="lable">
					<view class="lab-title">拉新任务 </view>
					<view class="addtask" @click="pull()">
						<image src="../../static/icon/addtask.png" mode=""></image>
					</view>
				</view>
				<view class="task" v-for="(item,key) in task3" :key="key" v-if="task3.length > 0">
					<view class="taskinfo">充值<input type="digit" class="input" v-model="item.where1"
							@input="onKeyInput" />元&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;奖励<input type="digit"
							class="input" v-model="item.reward" @input="reward" />%充值金</view>
				</view>

			</view>

			<view>
				<button type="warn" @click="submit">提交</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				formData: {
					where: "",
					value: ""
				},
				id: "",
				pid: '',
				task1: [],
				task2: [],
				task3: [],
				userData: '',
			}
		},
		onLoad(e) {
			this.pid = e.id
			let user = uni.getStorageSync('userData');
			this.userData = user;
			this.tasklist();
		},
		methods: {
			onKeyInput(e) {
				console.log('3-----', e.target.value)
			},
			reward(e) {
				console.log('4----', e.target.value)
			},



			async tasklist() {
				//金额  
				const task1 = await this.GQL({
					"operationName": "allSlProjectStoreTasks",
					"query": "query allSlProjectStoreTasks($filter: SlProjectStoreTaskFilter) { allSlProjectStoreTasks(filter:$filter) { nodes { createtime online pid reward tid type uid where1 } } }",
					"variables": {
						"filter": {
							"pid": {
								"equalTo": this.pid
							},
							"type": {
								"equalTo": "1"
							}
						}
					}
				})
				//订单  
				const task2 = await this.GQL({
					"operationName": "allSlProjectStoreTasks",
					"query": "query allSlProjectStoreTasks($filter: SlProjectStoreTaskFilter) { allSlProjectStoreTasks(filter:$filter) { nodes { createtime online pid reward tid type uid where1 } } }",
					"variables": {
						"filter": {
							"pid": {
								"equalTo": this.pid
							},
							"type": {
								"equalTo": "2"
							}
						}
					}
				})
				//拉新
				const task3 = await this.GQL({
					"operationName": "allSlProjectStoreTasks",
					"query": "query allSlProjectStoreTasks($filter: SlProjectStoreTaskFilter) { allSlProjectStoreTasks(filter:$filter) { nodes { createtime online pid reward tid type uid where1 } } }",
					"variables": {
						"filter": {
							"pid": {
								"equalTo": this.pid
							},
							"type": {
								"equalTo": "3"
							}
						}
					}
				})
				if (task1.data.allSlProjectStoreTasks.nodes != '') {
					this.task1 = task1.data.allSlProjectStoreTasks.nodes
				}
				if (task2.data.allSlProjectStoreTasks.nodes != '') {
					this.task2 = task2.data.allSlProjectStoreTasks.nodes
				}
				if (task3.data.allSlProjectStoreTasks.nodes != '') {
					this.task3 = task3.data.allSlProjectStoreTasks.nodes
				}


			},
			async submit() {
				let that = this
				const task2 = that.task2
				task2.forEach(function(item, index) {
					if (item.tid != '') { //更改原有任务
						that.uptask(item)
					} else { //新增任务
						that.addtasklist(item)
					}
				});
				const task1 = that.task1
				task1.forEach(function(item, index) {
					if (item.tid != '') { //更改原有任务
						that.uptask(item)
					} else { //新增任务
						that.addtasklist(item)
						// console.log('task1111',item)
					}
				});
				const task3 = that.task3
				task3.forEach(function(item, index) {
					if (item.tid != '') { //更改原有任务
						that.uptask(item)
					} else { //新增任务
						that.addtasklist(item)
					}
				});

				uni.showToast({
					title: '保存成功',
					icon: 'success',
					duration: 3000
				});

			},
			async uptask(row) {
				let tid = row.tid
				this.$delete(row, 'tid')
				const updata = await this.GQL({
					"operationName": "updateSlProjectStoreTaskByTid",
					"query": "mutation updateSlProjectStoreTaskByTid($input: UpdateSlProjectStoreTaskByTidInput!) { updateSlProjectStoreTaskByTid(input:$input) { slProjectStoreTask { tid } } }",
					"variables": {
						"input": {
							"tid": tid,
							"slProjectStoreTaskPatch": row
						}
					}
				})
			},
			async addtasklist(row) {
				this.$delete(row, 'tid')

				const add = await this.GQL({
					"operationName": "createSlProjectStoreTask",
					"query": "mutation createSlProjectStoreTask($input: CreateSlProjectStoreTaskInput!) { createSlProjectStoreTask(input: $input) { slProjectStoreTask { tid } } }",
					"variables": {
						"input": {
							"slProjectStoreTask": row
						}
					}
				})
			},


			//订单任务
			addItem() {
				let adda = {
					online: "1",
					type: "2",
					where1: "",
					reward: "",
					uid: this.userData.uid,
					pid: this.pid,
					tid: '',
				}
				console.log(adda)
				this.task2.push(adda)
			},
			online() {
				let adda = {
					online: "2",
					where1: "",
					type: "2",
					reward: "",
					uid: this.userData.uid,
					pid: this.pid,
					tid: '',
				}
				console.log(adda)
				this.task2.push(adda)
			},
			//金额任务
			priceAddItem() {
				let adda = {
					online: "1",
					where1: "",
					type: "1",
					reward: "",
					uid: this.userData.uid,
					pid: this.pid,
					tid: '',
				}
				console.log(adda)
				this.task1.push(adda)
			},
			priceAddItemonline() {
				let adda = {
					online: "2",
					where1: "",
					type: "1",
					reward: "",
					uid: this.userData.uid,
					pid: this.pid,
					tid: '',
				}
				console.log(adda)
				this.task1.push(adda)
			},
			//拉新
			pull() {
				let adda = {
					online: "1",
					where1: "",
					type: "3",
					reward: "",
					uid: this.userData.uid,
					pid: this.pid,
					tid: '',
				}
				console.log(adda)
				this.task3.push(adda)
			}
		}
	}
</script>

<style lang="scss">
	.u-page {
		background-color: #FBFCFE;
	}

	.content {
		margin: 15px;
	}

	.tasklist {
		background-color: #FFFFFF;
		height: auto;
		margin-bottom: 12px;
		box-shadow: 1px -2px 10px 6px rgba(0, 0, 0, 0.1);
		border-radius: 5px;

		.lable {
			display: flex;
			font-size: 15px;
			font-family: PingFangSC-Medium, PingFang SC;
			font-weight: 500;
			color: #767B8D;
			line-height: 16px;
			// padding: 16px;
			padding: 16px 16px 0px 16px;
		}
	}

	.task {
		display: flex;
		text-align: center;
	}

	.taskinfo {
		display: flex;
		padding: 16px;
		font-size: 12px;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: #161E3E;
		line-height: 30px;
	}

	.input {
		margin: 0px 10px;
		width: 62px;
		height: 31px;
		background: #F9F9F9;
		border-radius: 3px;
		border: 1px solid #D8D8D8;
	}

	.lab-title {
		padding: 10px 0px;
	}

	.addtask {
		text-align: right;
		height: 20px;
		padding: 10px;

		image {
			width: 20px;
			height: 20px;
		}
	}
</style>
